package com.yuke.cloud.service.omc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.omc.dto.GoodsInfo;
import com.yuke.cloud.service.api.omc.dto.OdsGoodSalesApiDto;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.omc.dao.OdsOrderGoodsMapper;
import com.yuke.cloud.service.omc.dao.OdsOrderMapper;
import com.yuke.cloud.service.omc.dto.OrderGoodsDto;
import com.yuke.cloud.service.omc.entity.OdsOrderGoods;
import com.yuke.cloud.service.omc.service.OdsOrderGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * Created by ssx on 2018/09/04.
 */
@Service
@Transactional
public class OdsOrderGoodsServiceImpl extends BaseService<OdsOrderGoods> implements OdsOrderGoodsService {
    @Resource
    private OdsOrderGoodsMapper odsOrderGoodsMapper;
    @Resource
    private OdsOrderMapper odsOrderMapper;
    @Resource
    private PmcFeignApi pmcFeignApi;




    public int save(OdsOrderGoods odsOrderGoods) {
        int save = super.save(odsOrderGoods);
        return save;
    }

    @Override
    public void savelist(List<OdsOrderGoods> orderGoods) {
        for (int i = 0; i < orderGoods.size(); i++) {
            super.save(orderGoods.get(i));
        }
    }

    @Override
    public List<Map> getcountnumbyshopandbyspecid(List<OdsOrderGoods> list) {
        List<Map> list1 = odsOrderGoodsMapper.getcountnumbyshopandbyspecid(list);
        return list1;
    }

    @Override
    public List<Map> selectnumbyshopidmonth(List<OdsOrderGoods> list) {
        List<Map> selectnumbyshopidmonth = odsOrderGoodsMapper.selectnumbyshopidmonth(list);
        return selectnumbyshopidmonth;
    }

    @Override
    public List<Map> countnum(List<Long> id) {
        List<Map> countnum = odsOrderGoodsMapper.countnum(id);

        return countnum;
    }

    @Override
    public List<Map> gethistorynum(Map<String, List<Long>> niubi) {
        List<Map> list2 = new ArrayList<>();
        if (niubi == null) {
            return list2;
        }
        if (niubi.get("shopId").get(0) == null || niubi.get("shopId").equals("")) {
            return list2;
        }
        Long shopId = niubi.get("shopId").get(0);
        List<Long> list = niubi.get("specIds");
        if (list == null || list.size() == 0) {
            return list2;
        }
        if (shopId == null || shopId.toString().equals("")) {
            return list2;
        }
        List<OdsOrderGoods> odsOrderGoods = new ArrayList<>();
        OdsOrderGoods odsOrderGoods1;
        for (int i = 0; i < list.size(); i++) {
            odsOrderGoods1 = new OdsOrderGoods();
            if (list.get(i) != null && !list.get(i).toString().equals("")) {
                odsOrderGoods1.setSpecId(list.get(i));
                odsOrderGoods1.setOrderId(shopId);//orderid存储shopid
                odsOrderGoods.add(odsOrderGoods1);
            }

        }
        if (odsOrderGoods.size() == 0) {
            return list2;
        }
        return odsOrderGoodsMapper.gethistorynum(odsOrderGoods);
    }

    @Override
    public List<Map> countmonthnumbyprimikey(List<GoodsInfo> shop_goods_id) {
        return odsOrderGoodsMapper.countmonthnumbyprimikey(shop_goods_id);
    }




    public long getsnowid() {
        long l = super.generateId();
        return l;
    }


    @Override
    public List<OrderGoodsDto>  selectByOrderId(Long orderId) {
        List<OrderGoodsDto> orderGoodsDtoList = odsOrderGoodsMapper.selectByOrderId(orderId);
        if (orderGoodsDtoList != null && orderGoodsDtoList.size() > 0){
            List<OrderGoodsDto> list =getOrderGoodsSpecInfo(orderGoodsDtoList);
            return list;
        }
        return orderGoodsDtoList;
    }

    @Override
    public List<OrderGoodsDto>  selectOrderGoodsByShopIds(List<Long> orderIds) {
        List<OrderGoodsDto> list = odsOrderGoodsMapper.selectOrderGoodsByShopIds(orderIds);
        return getOrderGoodsSpecInfo(list);
    }


    private List<OrderGoodsDto> getOrderGoodsSpecInfo(List<OrderGoodsDto> list){
        List<Long> specIds = list.stream().map(OrderGoodsDto::getSpecId).collect(Collectors.toList());

        List<ProdSpecInfoDto> prodSpecInfoDtos = pmcFeignApi.getSpecDtosBySpecIds(specIds);
        if (prodSpecInfoDtos != null && prodSpecInfoDtos.size() > 0 ){
            for (OrderGoodsDto orderGoodsDto : list){
                for (ProdSpecInfoDto item : prodSpecInfoDtos){
                    if (orderGoodsDto.getSpecId().equals(item.getSpecId())){
                        orderGoodsDto.setSpecName(item.getSpecName());
                        orderGoodsDto.setProductId(item.getProductId());
                        orderGoodsDto.setBigPic(item.getBigPic());
                    }

                }
            }

        }
        return list;
    }


    @Override
    public List<OdsGoodSalesApiDto> goodsSales(OdsGoodSalesApiDto odsGoodSalesApiDto) {
        System.out.println("odsGoodSalesApiDto"+odsGoodSalesApiDto);

        //出货的商品数量
        //订单状态已完成
        odsGoodSalesApiDto.setOrderType((byte)2);
        //商品出货总数量,总售价,总应收
        odsGoodSalesApiDto.setGoodsStatus((byte)0);

        List<OdsGoodSalesApiDto> list = odsOrderGoodsMapper.goodsSales(odsGoodSalesApiDto);

        //赠品的出货数量
        odsGoodSalesApiDto.setGoodsStatus((byte)4);

        List<OdsGoodSalesApiDto> giftList = odsOrderGoodsMapper.goodsSales(odsGoodSalesApiDto);


        for (OdsGoodSalesApiDto salesDto : list) {
            for (OdsGoodSalesApiDto dto : giftList) {
                if (salesDto.getSpecId().equals(dto.getSpecId())){
                    salesDto.setGiftNumber(dto.getNumber());
                }
            }
        }


        return list;
    }



}
